var Md5 = require('../../utils/md5.js');
var timeSecond = Math.floor(Date.now() * 0.001);  //获取时间戳: 
var SignInfoData = Md5.hexMD5(timeSecond + 'nativetalk')//md5加密;
var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
var httpNativetalk = app.globalData.httpNativetalk;
var httpWeike = app.globalData.httpWeike;
var teaId, couId, weekTime, reserve_start_time, reserve_end_time;
//  httpFile : 'http://file.waijiaojun.com/',
var day1Data = ['closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed'];
Page({
  data: {
    listData: ['09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '00:00', '01:00', '02:00', '03:00', '04:00', '05:00', '06:00', '07:00', '08:00'],
  },
  onShow: function (e) {

  },
  onLoad: function (e) {
    var that = this;
    teaId = e.teaId;
    couId = e.classId;
    console.log(e, e.teaId, e.date);
    //日期处理
    var week = new Date();
    var days = [];
    for (var i = week.getDay(); i <= 6; i++) {
      days.push(i);
    };
    var AA = 5 - days.length;
    if (days.length < 5) {
      for (var i = 0; i <= AA; i++) {
        days.push(i);
      }
    }
    var weekNum = days.slice(0);
    for (var i = 0; i <= 4; i++) {
      switch (days[i]) {
        case 0: days[i] = "周日"; break
        case 1: days[i] = "周一"; break
        case 2: days[i] = "周二"; break
        case 3: days[i] = "周三"; break
        case 4: days[i] = "周四"; break
        case 5: days[i] = "周五"; break
        case 6: days[i] = "周六"; break
      };
    };
    //第四,五天的日期    
    var day1Date = Date.parse(new Date());
    var day2Date = Date.parse(new Date()) + 86400000;
    var day3Date = Date.parse(new Date()) + 172800000;
    var day4Date = Date.parse(new Date()) + 259200000;
    var day5Date = Date.parse(new Date()) + 345600000;
    //时间戳处理函数   
    function getDate(time) {
      var date = new Date(time);
      var Y = date.getFullYear() + '-';
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
      var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
      var H = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() + ':';
      var Min = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
      return (M + D);
    };
    that.setData({
      days: days,
      day1Date: getDate(day1Date),
      day2Date: getDate(day2Date),
      day3Date: getDate(day3Date),
      day4Date: getDate(day4Date),
      day5Date: getDate(day5Date),
    });
    //老师开课时间的数据请求和处理
    wx.request({
      url: httpNativetalk + '/app/11/teacher/reservetime',
      data: {
        "cou_id": e.classId,
        "date": Date.parse(new Date()),
        "tea_id": e.teaId,
      },
      header: {
        "Timestamp": timeSecond,
        "SignInfo": SignInfoData,
        "content-Type": "application/json",
        "client_type": "3",
        "version": 15,
        'token': app.globalData.token,
      },
      method: "POST",
      success: function (res) {
        console.log(res);
        var dayData0 = ['closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed'];
        var dayData1 = ['closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed'];
        var dayData2 = ['closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed'];
        var dayData3 = ['closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed'];
        var dayData4 = ['closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed', 'closed'];
        that.setData({
          dayData0: dayData0,
          dayData1: dayData1,
          dayData2: dayData2,
          dayData3: dayData3,
          dayData4: dayData4,
        })
        // console.log(weekNum);
        for (var i = 0; i < weekNum.length; i++) {
          if (weekNum[i] == 0) {
            weekNum[i] = 7;
          }
        };
        // console.log(weekNum);
        console.log(res);
        weekTime = res.data.obj.weekReservetime;
        // console.log(weekTime);        
        for (var i = 0; i < weekNum.length; i++) {
          for (var j = 0; j < weekTime[weekNum[i]].length; j++) {
            var H = weekTime[weekNum[i]][j].reserve_time;
            var IsValid = weekTime[weekNum[i]][j].is_valid;
            var reserve_start_time = weekTime[weekNum[i]][j].date;
            // console.log(H);
            if (i == 0) {
              if (H >= 9) { dayData0.splice(H - 9, 1, 'open') } else {
                dayData0.splice(H + 15, 1, 'open')
              };
              if (H >= 9 && IsValid == 2) { dayData0.splice(H - 9, 1, 'booked') };
              if (H < 9 && IsValid == 2) { dayData0.splice(H + 15, 1, 'booked') }
            };
            if (i == 1) {
              if (H >= 9) { dayData1.splice(H - 9, 1, 'open') } else {
                dayData1.splice(H + 15, 1, 'open')
              }
              if (H >= 9 && IsValid == 2) { dayData1.splice(H - 9, 1, 'booked') };
              if (H < 9 && IsValid == 2) { dayData1.splice(H + 15, 1, 'booked') }
            };
            if (i == 2) {
              if (H >= 9) { dayData2.splice(H - 9, 1, 'open') } else {
                dayData1.splice(H + 15, 1, 'open')
              }
              if (H >= 9 && IsValid == 2) { dayData2.splice(H - 9, 1, 'booked') };
              if (H < 9 && IsValid == 2) { dayData2.splice(H + 15, 1, 'booked') }
            }
            if (i == 3) {
              if (H >= 9) { dayData3.splice(H - 9, 1, 'open') } else {
                dayData3.splice(H + 15, 1, 'open')
              }
              if (H >= 9 && IsValid == 2) { dayData3.splice(H - 9, 1, 'booked') };
              if (H < 9 && IsValid == 2) { dayData3.splice(H + 15, 1, 'booked') }
            }
            if (i == 4) {
              if (H >= 9) { dayData4.splice(H - 9, 1, 'open') } else {
                dayData4.splice(H + 15, 1, 'open')
              }
              if (H >= 9 && IsValid == 2) { dayData4.splice(H - 9, 1, 'booked') };
              if (H < 9 && IsValid == 2) { dayData4.splice(H + 15, 1, 'booked') }
            }

          }
        }
        that.setData({
          dayData0: dayData0,
          dayData1: dayData1,
          dayData2: dayData2,
          dayData3: dayData3,
          dayData4: dayData4,
        })

      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //点击open预约功能
  appointFun: function (e) {
    var that = this;
    var arr = e.currentTarget.dataset.arr;
    var num = e.currentTarget.dataset.daydata - 0;
    var idx = e.currentTarget.dataset.idx;
    var text = e.currentTarget.dataset.text;
    var wekNum = new Date().getDay();
    var Modal_day = e.currentTarget.dataset.dates;
    console.log(e);
    if (idx >= 14) {
      var hour = idx - 15;
    } else {
      var hour = idx + 9;
    };    
    if (wekNum + num > 7) {
      var wek_num = wekNum + num - 7;
    } else {
      var wek_num = wekNum + num;
    }
    for (var i = 0; i < weekTime[wek_num].length; i++) {
      console.log(hour);
      if (weekTime[wek_num][i].reserve_time == hour) {
        reserve_start_time = weekTime[wek_num][i].date;
        reserve_end_time = reserve_start_time + 3000000;
        console.log(reserve_start_time, reserve_end_time);
      }
    };
    if (text == 'open') {
      // arr.splice(idx, 1, "booked");
      // that.setData({
      //   ['dayData' + num]: arr,
      // })
      console.log(reserve_start_time, reserve_end_time);
      wx.showModal({
        title: '确认预约(' + hour + ':00)',
        content: '您将预约' + Modal_day + '日' + hour + ':00的课程,是否确认预约?',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定');
            wx.request({
              url: httpWeike + '/v4.3.1/student/appointment/token/add',
              data: {
                "cou_id": couId,
                "reserve_end_time": reserve_end_time,
                "reserve_start_time": reserve_start_time,
                "tea_id": teaId,
              },
              header: {
                "Timestamp": timeSecond,
                "SignInfo": SignInfoData,
                "content-Type": "application/json",
                "client_type": "3",
                "version": 15,
                'token': app.globalData.token,
              },
              method: 'POST',
              dataType: 'json',
              success: function (res) {
                if (res.data.mark == 1) {
                  wx.showToast({
                    title: res.data.tip,
                    icon: 'loading',
                    duration: 2000
                  })
                }
                if (res.data.mark == 0) {
                  wx.showToast({
                    title: res.data.tip,
                    icon: 'success',
                    duration: 2000
                  });
                  arr.splice(idx, 1, "booked");
                  that.setData({
                    ['dayData' + num]: arr,
                  })
                }
              },
              fail: function (res) { },
              complete: function (res) { },
            })

          } else if (res.cancel) {
            console.log('用户点击取消');
          }
        }
      })

    }

  },
  //查询是否购买该课程
  queryFun: function (e) {
    if (app.globalData.token !== null) {
      var that = this;
      wx.request({
        url: httpNativetalk + '/app/7/token/book/connect',
        data: {
          "currentPage": 1,
          "pageSize": 10,
        },
        header: {
          "Timestamp": timeSecond,
          "SignInfo": SignInfoData,
          "content-Type": "application/json",
          "client_type": "3",
          "version": 15,
          'token': app.globalData.token,
        },
        method: 'POST',
        success: function (res) {
          console.log(res);
        },
        fail: function (res) { },
        complete: function (res) { },
      })
    } else {
      wx.showToast({
        title: '请先绑定',
        icon: 'loading',
        duration: 2000
      });
    }
  }
})